package com.example.myapplication.Algorithm.maximumProduct;

import java.util.Arrays;

public class MaximumProduct {
        public static void main(String[] args){
            System.out.println(sort(new int[]{-1,2,3,-4,-5,-6,}));
            System.out.println(getMaxMin(new int[]{-1,2,3,-4,-5,-6,}));
        }

        ///左右最大的值的绝对值
        public static int sort(int[] nums){
            Arrays.sort(nums);
            int n = nums.length;
            return Math.max(nums[0] * nums[1] * nums[2] ,nums[n-1] * nums[n-2] * nums[n-3]);
        }

        ///线性扫描
        public static int getMaxMin(int[] nums){
            int min1 = Integer.MAX_VALUE,min2 = Integer.MAX_VALUE,min3 = Integer.MAX_VALUE;
            int max1 = Integer.MIN_VALUE,max2 = Integer.MIN_VALUE,max3 = Integer.MIN_VALUE;
            for (int x:nums){
                if(x<min1){
                    ///比最小的还要小
                    min3 = min2;
                    min2 = min1;
                    min1 = x;
                }else if(x<min2){
                    ///只比第二小的小
                    min3 = min2;
                    min2 = x;
                }else if(x<min3){
                    ///只比第二小的小
                    min3 = x;
                }

                if(x>max1){
                    ///比最大的还要大
                    max3 = max2;
                    max2 = max1;
                    max1 = x;
                }else if(x>max2){
                    ///只比第二大的大
                    max3 = max2;
                    max2 = x;
                }else if(x>max3){
                    ///只比第三大的大
                    max3 = x;
                }




            }
            System.out.println("min1 " +min1);
            System.out.println("min2 " +min2);
            System.out.println("min3 " +min3);
            System.out.println("max1 " +max1);
            System.out.println("max2 " +max2);
            System.out.println("max3 " +max3);
            return Math.max(min1 * min2 * min3 ,max1 * max2 * max3);
        }
    }